package shilin.test.demo.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import shilin.test.demo.domain.entity.Myuser;
import shilin.test.demo.service.IMyuserService;
import shilin.test.demo.util.JsonResult;
import shilin.test.demo.util.ResultCode;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoushilin
 * @since 2019-10-25
 */
@RestController
@RequestMapping("/test/myuser")
public class MyuserController {

    @Autowired
	private IMyuserService myuserService;

	@ApiOperation(value = "获得用户列表", notes = "1", httpMethod = "GET")
	@GetMapping(value = "/listUser")
	public JsonResult listUser() {
		// 框架自带的方法
		List<Myuser> list = this.myuserService.list(null);
		return new JsonResult(ResultCode.SUCCESS, list);
	}

	@ApiOperation(value = "根据用户名获得用户记录", notes = "名称不能为空", httpMethod = "GET")
	@ApiImplicitParam(dataType = "string", name = "name", value = "用户名", required = true)
	@GetMapping(value = "/getUserByName")
	public JsonResult getUserByName(String name) {
		// 自定义的方法
		Myuser myuser = this.myuserService.getUserByName(name);
		return new JsonResult(ResultCode.SUCCESS, myuser);
	}

	/**
	 * 根据用户id查找用户
	 * @param id 代表用户id
	 * @return 在页面上将用户信息已json文件的形式展现
	 */
	@GetMapping(value = "/getUserById")
	public JsonResult getUserById(Integer id){
		Myuser myuser = this.myuserService.getUserById(id);

		return new JsonResult(ResultCode.SUCCESS,myuser);
	}



	/**
	 * 添加用户操作
	 * @param id 用户id
	 * @param name 用户名称
	 * @param age 用户年龄
	 * @param email 用户email
	 */
	@GetMapping(value="/insert")
	public void insert(@Param("id") Integer id, @Param("name") String name, @Param("age") Integer age, @Param("email") String email){
		  this.myuserService.insert(id,name,age,email);

	}

	/**
	 * 根据用户id删除用户
	 * @param id id
	 * @return 成功1
	 */
	@GetMapping(value= "/deleteUserById")
	public int deleteUserById(Integer id){
		return this.myuserService.deleteUserById(id);
	}

	/**
	 * 根据用户id修改用户的信息
	 * @param id id
	 * @param name name
	 * @param age age
	 * @param email email
	 */
	@GetMapping(value = "/updateUserById")
	public void updateUserById(@Param("id") Integer id, @Param("name") String name, @Param("age") Integer age, @Param("email") String email){
		  this.myuserService.updateUserById(id,name,age,email);
	}




}
